package com.emergency.db.service.impl;

import com.emergency.common.util.CommonUtils;
import com.emergency.common.util.IdWorker;
import com.emergency.db.dao.PreplanDao;
import com.emergency.db.domain.PrePlan;
import com.emergency.db.service.PreplanService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Sort;
import org.springframework.data.jpa.domain.Specification;
import org.springframework.stereotype.Service;

import javax.persistence.criteria.CriteriaBuilder;
import javax.persistence.criteria.CriteriaQuery;
import javax.persistence.criteria.Predicate;
import javax.persistence.criteria.Root;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;

@Service
public class PreplanServiceImpl implements PreplanService {
    @Autowired
    private PreplanDao preplanDao;
    @Autowired
    private IdWorker idWorker;

    //保存
    public Long save(PrePlan preplan) {
        if (CommonUtils.isNullOrEmpty(preplan.getId())) {
            preplan.setId(idWorker.nextId());
            preplan.setCreateTime(new Date());
        }
        //保存预案基本信息
        preplanDao.save(preplan);
        return preplan.getId();
    }


    //查询单项
    public PrePlan findById(Long id) {
        return preplanDao.findById(id).get();
    }

    //删除
    public void delete(Long id) {
        preplanDao.deleteById(id);
    }

    //分页查询
    @Override
    public Page<PrePlan> findList(Map searchMap, int page, int size) {
        Specification<PrePlan> specification = createSpecification(searchMap);
        Sort sort = Sort.by(Sort.Direction.ASC, "id");
        PageRequest pageRequest = PageRequest.of(page - 1, size, sort);
        return preplanDao.findAll(specification, pageRequest);
    }

    /**
     * 动态条件构建
     *
     * @param searchMap
     * @return
     */
    private Specification<PrePlan> createSpecification(Map searchMap) {
        return new Specification<PrePlan>() {

            @Override
            public Predicate toPredicate(Root<PrePlan> root, CriteriaQuery<?> criteriaQuery, CriteriaBuilder criteriaBuilder) {
                List<Predicate> predicateList = new ArrayList<Predicate>();

                // 预案名称
                if (searchMap.get("prename") != null && !"".equals(searchMap.get("prename"))) {
                    predicateList.add(criteriaBuilder.like(root.get("prename").as(String.class), "%" + (String) searchMap.get("prename") + "%"));
                }
                // 预案编号
                if (searchMap.get("prenum") != null && !"".equals(searchMap.get("prenum"))) {
                    predicateList.add(criteriaBuilder.like(root.get("prenum").as(String.class), "%" + (String) searchMap.get("prenum") + "%"));
                }
                // 预案类型
                if (searchMap.get("type") != null && !"".equals(searchMap.get("type"))) {
                    predicateList.add(criteriaBuilder.like(root.get("type").as(String.class), "%" + (String) searchMap.get("type") + "%"));
                }
                return criteriaBuilder.and(predicateList.toArray(new Predicate[predicateList.size()]));
            }
        };
    }
}
